$(function()
{
	var lastDayClick="";
	var lastMonthClick="";
	var lastYearClick="";
	$('.dEnabled').click(function(e){
		if (e.shiftKey)
		{
			removeSelection();
			if(lastDayClick!="")
			{
				var ds=new Date(lastYearClick,lastMonthClick,lastDayClick);
				var de=new Date($(this).attr('year'),$(this).attr('month'),$(this).attr('day'));
				if(ds>de)
				{
					var t=ds;
					ds=de;
					de=t;
				}
				$('.dEnabled').each(function(){
					var dt=new Date($(this).attr('year'),$(this).attr('month'),$(this).attr('day'));
					if(dt>ds&&dt<de)
					{
						if($(this).hasClass('dChecked'))
						{
							$(this).removeClass('dChecked');
						}
						else
						{
							$(this).addClass('dChecked');
						}
					}
				});
				
			}
		}
		
		if($(this).hasClass('dChecked'))
		{
			$(this).removeClass('dChecked');
			lastDayClick="";
		}
		else
		{
			$(this).addClass('dChecked');
			lastDayClick=$(this).attr('day');
			lastMonthClick=$(this).attr('month');
			lastYearClick=$(this).attr('year');
		}
		
	});
	$('.dCheck').change(function(){
		if($(this).attr('checked')=='checked')
		{
			$('.dEnabled.dChecked').filter('[wday='+$(this).attr('val')+']').each(function(){
				if(!$(this).hasClass('dChecked')){$(this).addClass('dChecked');}
			});
			
		}
		else
		{
			$('.dEnabled').filter('[wday='+$(this).attr('val')+']').each(function(){
				if($(this).hasClass('dChecked')){$(this).removeClass('dChecked');}
			});
		}
	});


});

jQuery.fn.mcalendar = function(option){

	 var option=jQuery.extend({
		 trigger:'',
		 top:150,
		 left:50,
		 onSubmit:function(data){console.log(data);}
	 	},option);
	 	return this.each(function() 
	 			{
	 				$(this).html(	'<div class="mWdrag ui-widget ui-widget-content ui-corner-all ui-jqdialog"   style="overflow-x: hidden; overflow-y: hidden; top: '+option.top+'; left: '+option.left+';display:block; z-index: 960; width:560px" tabindex="-1" >'+
	 								'<div class="ui-jqdialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix" id="edithdtable1" style="cursor: move; ">'+
	 								'	<span class="ui-jqdialog-title" style="float: left; ">Календарь</span>'+
	 								'		<a href="javascript:void(0)" class="closeClass ui-corner-all" style="float:right">'+
	 								'			<span class="ui-icon ui-icon-closethick"></span>'+
	 								'		</a>'+
	 								'</div>'+
	 								'<div class="ui-jqdialog-content ui-widget-content" id="editcnttable1">'+
	 								'<span>'+
	 								'<div class="mwForm"  >'+
	 											createCalendar()+
	 								'</div>'+
	 								'<table border="0" cellspacing="0" cellpadding="0" class="EditTable" id="TblGrid_table1_2">'+
	 								'<tbody>'+
	 								'	<tr>'+
	 								'		<td colspan="2">'+
	 								'		<hr class="ui-widget-content" style="margin:1px">'+
	 								'		</td>'+
	 								'	</tr>'+
	 								'<tr id="Act_Buttons">'+
	 								'	<td class="navButton">'+
	 								'		<a href="javascript:void(0)" id="pData" class="fm-button ui-state-default ui-corner-left" style="display: none; ">'+
	 								'			<span class="ui-icon ui-icon-triangle-1-w"></span>'+
	 								'		</a>'+
	 								'		<a href="javascript:void(0)" id="nData" class="fm-button ui-state-default ui-corner-right" style="display: none; ">'+
	 								'			<span class="ui-icon ui-icon-triangle-1-e"></span>'+
	 								'		</a>'+
	 								'	</td>'+
	 								'	<td class="EditButton">'+
	 								'		<a href="javascript:void(0)" id="sData" class="submitClass fm-button ui-state-default ui-corner-all fm-button-icon-left">'+
	 								'			OK'+
	 								'			<span class="ui-icon ui-icon-check"></span>'+
	 								'		</a>'+
	 								'		<a href="javascript:void(0)" id="rData" class="resetClass fm-button ui-state-default ui-corner-all fm-button-icon-left">'+
	 								'			Сброс'+
	 								'			<span class="ui-icon ui-icon-refresh"></span>'+
	 								'		</a>'+
	 								'		<a href="javascript:void(0)" id="cData" class="closeClass fm-button ui-state-default ui-corner-all fm-button-icon-left">'+
	 								'			Отмена'+
	 								'			<span class="ui-icon ui-icon-close"></span>'+
	 								'		</a>'+
	 								'	</td>'+
	 								'</tr>'+
	 								'<tr style="display: none; " class="binfo">'+
	 								'	<td class="bottominfo" colspan="2">	</td>'+
	 								'</tr>'+
	 								'</tbody>'+
	 								'</table>'+
	 								'</span>'+
	 								'</div>'+
	 								'</div>');

	 				var temp=this;
	 				this.setHeight=function(h)
	 				{
	 					option.height=h;
	 				}
	 				$('.resetClass',this).click(function(){
	 					$('.dChecked').removeClass('dChecked');
	 				});
	 				$(this).css('display','none').css('position','absolute');
	 				$('.mWdrag',this).draggable({handle:'.ui-widget-header'}); 
	 				
	 				$('.'+option.trigger).click(function(){
	 					$(temp).toggle();
	 				});
	 				$('.closeClass',this).click(function(){
	 					$(temp).css('display','none');
	 				});
	 				$('.submitClass',this).click(function(){
	 					var data=[];
	 					$('.dChecked').each(function(){
	 						data.push($(this).attr('year')+"-"+$(this).attr('month')+"-"+$(this).attr('day'));
	 					});
	 					
	 					option.onSubmit(data.join(';'));
		 	 			$(temp).css('display','none');
		 	 		});
	 				
	 				
	 				
	 			});
	 	
	};



function createCalendar()
{
	var months=["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"];
	var cal="<table style='background:#fff' cellpadding=5 cellspacing=0>";
	var d=new Date();
	var curMonth=d.getMonth();
	var year=parseInt(d.getFullYear());
	for(var i=0;i<3;i++)
	{
		cal+="<tr>"
		for(var j=0;j<4;j++)
		{
			cal+="<td valign=top><div class='mName' style='text-align:center'>"+months[curMonth]+"</div><div class='mMonth'>";"</div></td>";
			cal+=createMoth(curMonth,year)
			curMonth++;
			if(curMonth>11)
			{
				curMonth=0;
				year++;
			}
		}
		cal+="</tr>";
	}
	cal+="</table><div style='margin-top:20px'>";
	cal+="<input type=checkbox class='dCheck' val='1' checked><span class=d_o_w>Пн</span>";
	cal+="<input type=checkbox class='dCheck' val='2' checked><span class=d_o_w>Вт</span>";
	cal+="<input type=checkbox class='dCheck' val='3' checked><span class=d_o_w>Ср</span>";
	cal+="<input type=checkbox class='dCheck' val='4' checked><span class=d_o_w>Чт</span>";
	cal+="<input type=checkbox class='dCheck' val='5' checked><span class=d_o_w>Пт</span>";
	cal+="<input type=checkbox class='dCheck' val='6' checked><span class=d_o_w>Сб</span>";
	cal+="<input type=checkbox class='dCheck' val='7' checked><span class=d_o_w>Вс</span></div>";
	return cal;
}
function createMoth(m,y)
{
	var d=new Date(y,m,1);
	var now=new Date();
	var cal="<table cellpadding=0 cellspacing=0>";
	var daysA=['Пн','Вт','Ср','Чт','Пт','Сб','Вс'];
	var m3=m+1;
	var days=(m3!=2?((m3%2)^(m3>7))+30:(!(y%400)||!(y%4)&&(y%25)?29:28));
	if(m3==1)
	{
		var m2=12;
		var y2=y-1;
		var beforM=(m2!=2?((m2%2)^(m2>7))+30:(!(y2%400)||!(y2%4)&&(y2%25)?29:28));
	}
	else
	{
		var m2=m3-1;
		var y2=y;
		var beforM=(m2!=2?((m2%2)^(m2>7))+30:(!(y2%400)||!(y2%4)&&(y2%25)?29:28));
	}
		
	var dayStart=new Date(y,m,1).getDay();
	if(dayStart==0){dayStart=7;}
	var dayEnd=new Date(y,m,days).getDay();
	if(dayEnd==0){dayEnd=7;}
	var column=0;
	
	for(var i=2-dayStart;i<=days+7-dayEnd;i++)
	{
		if(column==0)
		{
			cal+="<tr>";
		}
		cal+="<td style='margin:0;padding:0;width:25px;height:13px'>";
		if(i<=0)cal+="<div class='dDisabled'>"+(beforM+i)+"</div>";
		else if(i>days) cal+="<div class='dDisabled'>"+(i-days)+"</div>";
		else 
		{
			//console.log(new Date(y,m,i)>=now);
			if(new Date(y,m,i+1)>now)
			{
				cal+="<div class='dEnabled' day='"+i+"' month='"+m3+"' year='"+y+"' wday='"+(column+1)+"'>"+i+"</div>";
			}
			else
			{
				cal+="<div class='dMinus'>"+i+"</div>";
			}
		}
		cal+="</td>";
		column++
		if(column==7)
		{
			column=0;
			cal+="</tr>";
		}
	}
	cal+="</table>";
	return cal;
}
function removeSelection()
{
    if (window.getSelection) { window.getSelection().removeAllRanges(); }
    else if (document.selection && document.selection.clear)
      document.selection.clear();
 }